package ar.uba.fi.tonyvaliente;

import java.util.LinkedList;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.NotFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.NodeIterator;
import org.htmlparser.util.ParserException;
import org.htmlparser.util.SimpleNodeIterator;

import ar.uba.fi.tonyvaliente.text.extraction.NodeExtractor;
import ar.uba.fi.tonyvaliente.text.extraction.TextExtractor;
import ar.uba.fi.tonyvaliente.text.filtering.AcutesFilter;
import ar.uba.fi.tonyvaliente.text.filtering.CaseFoldingFilter;
import ar.uba.fi.tonyvaliente.text.filtering.SkeepDuplicatedFilter;
import ar.uba.fi.tonyvaliente.text.filtering.StemmingFilter;
import ar.uba.fi.tonyvaliente.text.filtering.StopWordsFilter;
import ar.uba.fi.tonyvaliente.text.filtering.TermsFilter;
import ar.uba.fi.tonyvaliente.text.filtering.TextFilter;

public class MainGabi {

	public static void main(String[] args) {
		Parser parser = new Parser();
		NodeList tags;
		try {
			parser
					.setResource("C:\\es.wikipedia.org\\Historia_de_la_electricidad.htm");
			tags = parser.parse(null);
		} catch (ParserException e) {
			e.printStackTrace();
			return;
		}

		NodeList headList = new NodeList();
		try {
			NodeFilter filter = new TagNameFilter("HEAD");
			for (NodeIterator it = tags.elements(); it.hasMoreNodes();)
				it.nextNode().collectInto(headList, filter);
		} catch (ParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		NodeList titleList = new NodeList();
		try {
			NodeFilter filter = new TagNameFilter("TITLE");
			for (NodeIterator it = headList.elements(); it.hasMoreNodes();)
				it.nextNode().collectInto(titleList, filter);
		} catch (ParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		for (SimpleNodeIterator it = titleList.elements(); it.hasMoreNodes();) {
			System.out.println(it.nextNode().toString());
		}
		
		NodeList bodyList = new NodeList();
		try {
			NodeFilter filter = new TagNameFilter("BODY");
			for (NodeIterator it = tags.elements(); it.hasMoreNodes();)
				it.nextNode().collectInto(bodyList, filter);
		} catch (ParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		for (SimpleNodeIterator it = bodyList.elements(); it.hasMoreNodes();) {
			System.out.println(it.nextNode().toString());
		}

		
		// NodeList filteredNodes = performNodeFiltering(bodyChilds);
		// LinkedList<String> extractedText =
		// performTextExtraction(filteredNodes);
		// // showList(extractedText, "text extraction 1", "text extraction 2");
		// System.out.println("comienza el filtrado");

		// LinkedList<String> filteredText =
		// performTextFiltering(extractedText);
		// showList(filteredText, "text filtering 1", "text filtering 2");
		// System.out.println("termina el filtrado");

	}

	private static void showList(LinkedList<String> list, String before,
			String after) {
		System.out.println(before);
		for (String s : list) {
			System.out.println(s);
		}
		System.out.println(after);
	}

	private static NodeList performNodeFiltering(NodeList nodes) {
		nodes.keepAllNodesThatMatch(new NotFilter(new TagNameFilter("script")),
				true);
		return nodes;
	}

	private static LinkedList<String> performTextExtraction(NodeList nodes) {
		LinkedList<NodeExtractor> extractors = new LinkedList<NodeExtractor>();
		extractors.add(new TextExtractor());

		return performTextExtraction(nodes, extractors);
	}

	private static LinkedList<String> performTextExtraction(NodeList nodes,
			LinkedList<NodeExtractor> extractors) {
		LinkedList<String> extracted = new LinkedList<String>();
		for (SimpleNodeIterator nodesIter = nodes.elements(); nodesIter
				.hasMoreNodes();) {
			Node node = nodesIter.nextNode();
			for (NodeExtractor extractor : extractors) {
				LinkedList<String> result = extractor.extract(node);
				if (result != null) {
					extracted.addAll(result);
				}
			}

			NodeList childrens = node.getChildren();
			if (childrens != null) {
				LinkedList<String> result = performTextExtraction(childrens,
						extractors);
				if (result != null) {
					extracted.addAll(result);
				}
			}
		}
		return extracted;
	}

	private static LinkedList<String> performTextFiltering(
			LinkedList<String> extractedText) {
		LinkedList<TextFilter> filters = new LinkedList<TextFilter>();
		// filters.add(new TermsFilter());
		// filters.add(new CaseFoldingFilter());
		// filters.add(new AcutesFilter());
		// filters.add(new SkeepDuplicatedFilter());
		filters.add(new StopWordsFilter());
		// filters.add(new StemmingFilter());

		return performTextFiltering(extractedText, filters);
	}

	private static LinkedList<String> performTextFiltering(
			LinkedList<String> extractedText, LinkedList<TextFilter> filters) {

		LinkedList<String> filtered = extractedText;

		for (TextFilter filter : filters) {
			System.out.println("");
			System.out.println("");
			System.out.println(filter.getClass().getName());
			System.out.println("antes de filter");
			System.out.println(filtered.size());
			filtered = filter.filter(filtered);
			System.out.println("despues de filter");
			System.out.println(filtered.size());

		}

		return filtered;
	}

}
